﻿using SheepStorage.Domain.DTO.Output;
using SheepStorage.Domain.Entity;
using SheepStorage.Repository.Interface;
using SheepStorage.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SheepStorage.Service.DataImp
{
    public class ProductionStatisticsService : IProductionStatisticsService
    {
        IBaseRepository<ProgenyTransfer> _ptres;//后裔转种
        IBaseRepository<SheepPurchase> _spres;//采购
        IBaseRepository<SheepDeath> _sdres;//死亡
        IBaseRepository<SheepOut> _sores;//淘汰
        IBaseRepository<SheepMomHybridization> _smres;//配种
        IBaseRepository<SheepMomDelivery> _mdres;//分娩
        IBaseRepository<SheepMomWeaning> _mwres;//种母断奶

        public ProductionStatisticsService(IBaseRepository<ProgenyTransfer> ptres, IBaseRepository<SheepPurchase> spres,
            IBaseRepository<SheepDeath> sdres, IBaseRepository<SheepOut> sores, IBaseRepository<SheepMomHybridization> smres,
            IBaseRepository<SheepMomDelivery> mdres, IBaseRepository<SheepMomWeaning> mwres)
        {
            _ptres = ptres;//后裔转种
            _spres = spres;//采购
            _sdres = sdres;//死亡
            _sores = sores;//淘汰
            _smres = smres;//配种
            _mdres = mdres;//分娩
            _mwres = mwres;
        }

        /// <summary>
        /// 种母存栏变动
        /// </summary>
        /// <param name="dates"></param>
        /// <returns></returns>
        public ProductionVariationOutput ShowAlternateTransplanting(string? dates)
        {
            var pvoutput=new ProductionVariationOutput();
            var list = _ptres.GetAll().Where(x=>x.TransferSex==false&&x.IsDel==0).ToList();
            var cglist = _spres.GetAll().Where(x => x.PurchaseSheepType == 2 && x.IsDel == 0).ToList();
            var swlist = _sdres.GetAll().Where(x => x.SheepSaleType == 2 && x.IsDel == 0).ToList();
            var ttlist = _sores.GetAll().Where(x => x.SheepSaleType == 2 && x.IsDel == 0).ToList();
            var pzlist = _smres.GetAll().Where(x=>x.IsDel==0).ToList();//配种
            if (dates.Length == 4)
            {
                list = list.Where(x =>x.AddTime!=null&& x.AddTime.Value.Year.ToString()== dates).ToList();
                cglist = cglist.Where(x => x.PurchaseDate != null && x.PurchaseDate.Value.Year.ToString() == dates).ToList();
                swlist = swlist.Where(x => x.SheepDeathDate.Year.ToString() == dates).ToList();
                ttlist = ttlist.Where(x => x.AddTime != null && x.AddTime.Value.Year.ToString() == dates).ToList();
                pzlist = pzlist.Where(x => x.MomHybridizationDate != null && x.MomHybridizationDate.Value.Year.ToString() == dates).ToList();
            }
            if (dates.Length == 7)
            {
                list = list.Where(x => x.AddTime != null && x.AddTime.Value.ToString("yyyy-MM") == dates).ToList();
                cglist = cglist.Where(x => x.PurchaseDate != null && x.PurchaseDate.Value.ToString("yyyy-MM") == dates).ToList();
                swlist = swlist.Where(x => x.SheepDeathDate.ToString("yyyy-MM") == dates).ToList();
                ttlist = ttlist.Where(x => x.AddTime != null && x.AddTime.Value.ToString("yyyy-MM") == dates).ToList();
                pzlist = pzlist.Where(x => x.MomHybridizationDate != null && x.MomHybridizationDate.Value.ToString("yyyy-MM") == dates).ToList();
            }
            if (dates.Length == 10)
            {
                list = list.Where(x => x.AddTime != null && x.AddTime.Value.ToString("yyyy-MM-dd") == dates).ToList();
                cglist = cglist.Where(x => x.PurchaseDate != null && x.PurchaseDate.Value.ToString("yyyy-MM-dd") == dates).ToList();
                swlist = swlist.Where(x => x.SheepDeathDate.ToString("yyyy-MM-dd") == dates).ToList();
                ttlist = ttlist.Where(x => x.AddTime != null && x.AddTime.Value.ToString("yyyy-MM-dd") == dates).ToList();
                pzlist = pzlist.Where(x => x.MomHybridizationDate != null && x.MomHybridizationDate.Value.ToString("yyyy-MM-dd") == dates).ToList();
            }
            pvoutput.AlternateTransplanting = list.Count();
            pvoutput.Purchase = cglist.Count();
            pvoutput.Die=swlist.Count();
            pvoutput.Eliminate = ttlist.Count();
            pvoutput.GetHybridization=pzlist.Count();

            return pvoutput;
        }

        /// <summary>
        /// 羔羊存栏
        /// </summary>
        /// <param name="dates"></param>
        /// <returns></returns>
        public LambLivestockOutput ShowLambLivestock(string dates) 
        {
            var pvoutput = new LambLivestockOutput();
            var list = _mdres.GetAll().Where(x =>  x.IsDel == 1).ToList();//分娩
            int? cg = 0;//产羔
            //采购
            var cglist = _spres.GetAll().Where(x => x.PurchaseSheepType == 3 && x.IsDel == 0).ToList();
            //死亡
            var swlist = _sdres.GetAll().Where(x => x.SheepSaleType == 3 && x.IsDel == 0).ToList();
            //淘汰
            var ttlist = _sores.GetAll().Where(x => x.SheepSaleType == 3 && x.IsDel == 0).ToList();
            if (dates.Length == 4)
            {
                //产羔
                list = list.Where(x => x.MomDeliveryDate != null && x.MomDeliveryDate.Value.Year.ToString() == dates).ToList();
                foreach (var item in list)
                {
                    cg += item.DeliveryTotalLiving;
                }
                //采购
                cglist = cglist.Where(x => x.PurchaseDate != null && x.PurchaseDate.Value.Year.ToString() == dates).ToList();
                //死亡
                swlist = swlist.Where(x => x.SheepDeathDate.Year.ToString() == dates).ToList();
                //淘汰
                ttlist = ttlist.Where(x => x.AddTime != null && x.AddTime.Value.Year.ToString() == dates).ToList();
            }
            if (dates.Length == 7)
            {
                //产羔
                list = list.Where(x => x.MomDeliveryDate != null && x.MomDeliveryDate.Value.ToString("yyyy-MM") == dates).ToList();
                foreach (var item in list)
                {
                    cg += item.DeliveryTotalLiving;
                }
                cglist = cglist.Where(x => x.PurchaseDate != null && x.PurchaseDate.Value.ToString("yyyy-MM") == dates).ToList();
                swlist = swlist.Where(x => x.SheepDeathDate.ToString("yyyy-MM") == dates).ToList();
                ttlist = ttlist.Where(x => x.AddTime != null && x.AddTime.Value.ToString("yyyy-MM") == dates).ToList();
            }
            if (dates.Length == 10)
            {
                //产羔
                list = list.Where(x => x.MomDeliveryDate != null && x.MomDeliveryDate.Value.ToString("yyyy-MM-dd") == dates).ToList();
                foreach (var item in list)
                {
                    cg += item.DeliveryTotalLiving;
                }
                cglist = cglist.Where(x => x.PurchaseDate != null && x.PurchaseDate.Value.ToString("yyyy-MM-dd") == dates).ToList();
                swlist = swlist.Where(x => x.SheepDeathDate.ToString("yyyy-MM-dd") == dates).ToList();
                ttlist = ttlist.Where(x => x.AddTime != null && x.AddTime.Value.ToString("yyyy-MM-dd") == dates).ToList();
            }
            pvoutput.Lambing = cg??0;
            pvoutput.Delivery = cglist.Count();
            pvoutput.Die=swlist.Count();
            pvoutput.Eliminate = ttlist.Count();

            return pvoutput;
        }

        /// <summary>
        /// 分娩统计
        /// </summary>
        /// <param name="dates"></param>
        /// <returns></returns>
        public GetDeliveryOutput ShowDelivery(string dates)
        {
            var pvoutput = new GetDeliveryOutput();
            var list = _mdres.GetAll().Where(x => x.IsDel == 1).ToList();//分娩
            int? ws = 0;//窝数
            int? hg = 0;//活羔
            int? sg = 0;//死羔
            if (dates.Length == 4)
            {
                //产羔
                list = list.Where(x => x.MomDeliveryDate != null && x.MomDeliveryDate.Value.Year.ToString() == dates).ToList();
                foreach (var item in list)
                {
                    ws += item.MomDeliveryCount;
                    hg += item.DeliveryTotalLiving;
                    sg += item.DeliveryTotalDie;
                }
            }
            if (dates.Length == 7)
            {
                //产羔
                list = list.Where(x => x.MomDeliveryDate != null && x.MomDeliveryDate.Value.ToString("yyyy-MM") == dates).ToList();
                foreach (var item in list)
                {
                    ws += item.MomDeliveryCount;
                    hg += item.DeliveryTotalLiving;
                    sg += item.DeliveryTotalDie;
                }
            }
            if (dates.Length == 10)
            {
                //产羔
                list = list.Where(x => x.MomDeliveryDate != null && x.MomDeliveryDate.Value.ToString("yyyy-MM-dd") == dates).ToList();
                foreach (var item in list)
                {
                    ws += item.MomDeliveryCount;
                    hg += item.DeliveryTotalLiving;
                    sg += item.DeliveryTotalDie;
                }
            }
            pvoutput.Sum=(hg+sg)??0;//总数
            pvoutput.LiveSun = hg ?? 0;
            pvoutput.DieSum = sg ?? 0;
            pvoutput.NestSum = ws ?? 0;


            return pvoutput;
        }

        /// <summary>
        /// 断奶统计
        /// </summary>
        /// <param name="dates"></param>
        /// <returns></returns>
        public WeaningOutput ShowWeaning(string dates)
        {
            var pvoutput = new WeaningOutput();
            var list = _mwres.GetAll().Where(x => x.IsDel == 0).ToList();//断奶
            int? ws = 0;//窝数
            decimal? zz = 0;//总重
            if (dates.Length == 4)
            {
                //产羔
                list = list.Where(x => x.WeaningDate != null && x.WeaningDate.Value.Year.ToString() == dates).ToList();
                foreach (var item in list)
                {
                    ws ++;
                    zz += item.WeaningWeight;
                }
            }
            if (dates.Length == 7)
            {
                //产羔
                list = list.Where(x => x.WeaningDate != null && x.WeaningDate.Value.ToString("yyyy-MM") == dates).ToList();
                foreach (var item in list)
                {
                    ws++;
                    zz += item.WeaningWeight;
                }
            }
            if (dates.Length == 10)
            {
                //产羔
                list = list.Where(x => x.WeaningDate != null && x.WeaningDate.Value.ToString("yyyy-MM-dd") == dates).ToList();
                foreach (var item in list)
                {
                    ws++;
                    zz += item.WeaningWeight;
                }
            }
            if (ws == 0)
            {
                return pvoutput;
            }
            pvoutput.nest = ws??0;//总数
            pvoutput.sumweight=zz??0;//总重
            pvoutput.avgweight=(zz/ws)??0;//均重

            return pvoutput;
        }




    }
}
